package com.itheima.util;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class EncodingFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        // 过滤请求
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;

        /*String remoteAddr = request.getRemoteAddr();
        System.out.println("请求者的ip地址为: "+remoteAddr);
        String[] ipaddr = new String[]{"192.168.142","192.168.143"};
        for (String ip : ipaddr) {
            if(remoteAddr.startsWith(ip)){
                response.sendRedirect(request.getContextPath()+"/error/error.jsp");
                return;
            }
        }*/
        // 获取请求方式
        String method = request.getMethod();
        if("POST".equalsIgnoreCase(method)){
            request.setCharacterEncoding("utf-8");
        }
        chain.doFilter(request, response);
        // 过滤响应

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
